const axios = require('axios');
const { MediaCheck } = require('../db');
module.exports = {
  commonController(app) {
    // 消息接收
    app.all('/receiveMsg', async (req, res) => {
      const headers = req.headers;
      // const weixinAPI = `http://api.weixin.qq.com/cgi-bin/message/custom/send`;
      // const payload = {
      //   touser: headers['x-wx-openid'],
      //   msgtype: 'text',
      //   text: {
      //     content: `云托管接收消息推送成功，内容如下：\n${ JSON.stringify(req.body, null, 2) }`,
      //   },
      // };
      try {
        // dispatch to wx server
        // const result = await axios.post(weixinAPI, payload);
        console.log('🚀 ~ 消息接收 req.body', JSON.stringify(req.body || '{}'));
        // console.log('🚀 ~ 消息接收 result.data', JSON.stringify(result.data || '{}'));
        if (req.body.errcode === 0) {
          const { trace_id } = req.body;
          await MediaCheck.update({
            result: JSON.stringify(req.body || {}),
          }, {
            where: { traceId: trace_id },
          });
        } else {
        }
        res.send('success');
      } catch (e) {
        console.error('❌ ~ 消息接收 e', e);
        res.send('fail');
      }
    });
  },
};
